package com.iti.jets.gpts.main;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import com.iti.jets.gpts.home.AttributeTypeHome;
import com.iti.jets.gpts.home.TranServiceTypeAttributeHome;
import com.iti.jets.gpts.home.TranServiceTypeAttributeTypeHome;
import com.iti.jets.gpts.home.TranServiceTypeHome;
import com.iti.jets.gpts.pojo.AttributeType;
import com.iti.jets.gpts.pojo.TranServiceType;
import com.iti.jets.gpts.pojo.TranServiceTypeAttribute;
import com.iti.jets.gpts.pojo.TranServiceTypeAttributeId;
import com.iti.jets.gpts.pojo.TranServiceTypeAttributeType;
import java.util.ArrayList;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author mustafajnr
 */
public class MustafaMain {

    static SessionFactory sessionFactory;
    static AttributeTypeHome atDAO;
    static TranServiceTypeHome tstDAO;
    static TranServiceTypeAttributeHome tstaDAO;
    static TranServiceTypeAttributeTypeHome tstatDAO;

    static {
        sessionFactory = new Configuration().configure("com/iti/jets/gpts/config/hibernate.cfg.xml")
                .buildSessionFactory();
        atDAO = new AttributeTypeHome(sessionFactory);
        tstDAO = new TranServiceTypeHome(sessionFactory);
        tstaDAO = new TranServiceTypeAttributeHome(sessionFactory);
        tstatDAO = new TranServiceTypeAttributeTypeHome(sessionFactory);
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void setSessionFactory(SessionFactory sessionFactory) {
        MustafaMain.sessionFactory = sessionFactory;
    }

    public static AttributeTypeHome getAtDAO() {
        return atDAO;
    }

    public static void setAtDAO(AttributeTypeHome atDAO) {
        MustafaMain.atDAO = atDAO;
    }

    public static TranServiceTypeHome getTstDAO() {
        return tstDAO;
    }

    public static void setTstDAO(TranServiceTypeHome tstDAO) {
        MustafaMain.tstDAO = tstDAO;
    }

    public static TranServiceTypeAttributeHome getTstaDAO() {
        return tstaDAO;
    }

    public static void setTstaDAO(TranServiceTypeAttributeHome tstaDAO) {
        MustafaMain.tstaDAO = tstaDAO;
    }

    public static TranServiceTypeAttributeTypeHome getTstatDAO() {
        return tstatDAO;
    }

    public static void setTstatDAO(TranServiceTypeAttributeTypeHome tstatDAO) {
        MustafaMain.tstatDAO = tstatDAO;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        // Create Operations
        for (AttributeType at : atDAO.loadAll()) {
            System.out.println(at);
        }
        atDAO.create(new AttributeType("AttributeType1", false));
    }

    static int addAttributeTypeToServiceType(int tstId, int atId) {
        TranServiceType tst = tstDAO.read(tstId);
        if (tst == null) {
            return -1;
        }
        AttributeType at = atDAO.read(atId);
        if (at == null) {
            return -2;
        }
        TranServiceTypeAttributeType tstat
                = new TranServiceTypeAttributeType(at, tst);
        if (tstatDAO.exists(tstat)) return -4;
        tstatDAO.create(tstat);
        return tstat.getId();
    }

    static TranServiceTypeAttributeId addAttributeToServiceType(int tstId, int atId, String value) {
        TranServiceType tst = tstDAO.read(tstId);
        if (tst == null) {
            return new TranServiceTypeAttributeId(0, -1);
        }
        AttributeType at = atDAO.read(atId);
        if (at == null) {
            return new TranServiceTypeAttributeId(-1, 0);
        }
        if (at.getIsNumber()) {
            try {
            Integer.parseInt(value);
            } catch (NumberFormatException nfe) {
                return new TranServiceTypeAttributeId(0, 0);
            }
        }
        TranServiceTypeAttributeId tstaId =
                new TranServiceTypeAttributeId(atId, tstId);
        TranServiceTypeAttribute tsta
                = new TranServiceTypeAttribute(tstaId, tst, at);
        tstaDAO.create(tsta);
        return tsta.getId();
    }
}
